/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.connDB4o;

import com.db4o.*;
import com.db4o.query.Query;
import com.constants.Constant;
import qlytv.QLyTVView;
import com.datatype.*;

/**
 *
 * @author Dane
 */
public class ConnectDB4o {

    /**
     * Create and Open db
     *
     * @return db40
     */
    public static ObjectContainer connDB4o(String filename) {
        ObjectContainer db = null;
        try {

            switch (filename) {

                //Database NHANVIEN
                case Constant.DB4OFILENAME_NHANVIEN:
                    db = Db4o.openFile(Constant.DB4OFILENAME_NHANVIEN);
                    break;

                //Database PHONGBAN
                case Constant.DB4OFILENAME_PHONGBAN:
                    db = Db4o.openFile(Constant.DB4OFILENAME_PHONGBAN);
                    break;

                //Database THE
                case Constant.DB4OFILENAME_THE:
                    db = Db4o.openFile(Constant.DB4OFILENAME_THE);
                    break;

                //Database DOCGIA
                case Constant.DB4OFILENAME_DOCGIA:
                    db = Db4o.openFile(Constant.DB4OFILENAME_DOCGIA);
                    break;

                //Default Database    
                default:
                    break;

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return db;
    }

    /**
     * insert data
     */
    public static String insertData(String database, NhanVien nv, Phongban pb, The the, Docgia dg) {
        ObjectContainer db = null;
        String flag = "";
        try {
            switch (database) {

                //Insert NHANVIEN
                case Constant.DB4OFILENAME_NHANVIEN:

                    //Check NhanVien
                    Boolean check = chkData(database, nv.getMaNV());
                    //Check PhongBan
                    Boolean check_ = chkData(Constant.DB4OFILENAME_PHONGBAN, nv.getPhongban());

                    //open data
                    db = connDB4o(Constant.DB4OFILENAME_NHANVIEN);

                    //insert
                    if (check.booleanValue()) {

                        QLyTVView.showMessage("Mã nhân viên:" + nv.getMaNV() + " đã tồn tại");
                        flag = "";

                    } else if (!check_.booleanValue()) {

                        QLyTVView.showMessage("Mã phòng ban:" + nv.getPhongban() + " không tồn tại");
                        flag = "";

                    } else {
                        db.store(nv);
                        db.close();
                        flag = "X";
                    }
                    break;

                //Insert PHONGBAN
                case Constant.DB4OFILENAME_PHONGBAN:

                    //check Phongban
                    Boolean check_pb = chkData(database, pb.getMaPB());

                    //open data
                    db = connDB4o(Constant.DB4OFILENAME_PHONGBAN);

                    //insert
                    if (check_pb.booleanValue()) {

                        QLyTVView.showMessage("Mã phòng ban:" + pb.getMaPB() + " đã tồn tại");
                        flag = "";

                    } else {
                        db.store(pb);
                        db.close();
                        flag = "X";
                    }
                    break;

                //Insert THE
                case Constant.DB4OFILENAME_THE:

                    //check The
                    Boolean check_the = chkData(database, the.getMathe());

                    //open data
                    db = connDB4o(database);

                    //insert
                    if (check_the.booleanValue()) {

                        QLyTVView.showMessage("Mã thẻ:" + the.getMathe() + " đã tồn tại");
                        flag = "";

                    } else {
                        db.store(the);
                        db.close();
                        flag = "X";
                    }

                    break;

                //Insert DOCGIA
                case Constant.DB4OFILENAME_DOCGIA:

                    //open data
                    db = connDB4o(database);

                    //insert
                    db.store(dg);
                    db.close();
                    flag = "X";

                    break;

                //Default Insert    
                default:
                    break;

            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            db.close();
        }
        return flag;
    }

    /**
     * check data
     *
     * @param manv
     * @return
     */
    public static Boolean chkData(String database, String id) {

        Boolean check = false;
        ObjectContainer db = null;
        try {
            switch (database) {

                //Check Database NHANVIEN
                case Constant.DB4OFILENAME_NHANVIEN:
                    //open data
                    db = connDB4o(database);

                    ObjectSet<NhanVien> result = db.query(NhanVien.class);

                    while (result.hasNext()) {

                        NhanVien nv = (NhanVien) result.next();

                        if (nv.getMaNV().equals(id)) {
                            check = true;
                            break;
                        }
                    }
                    break;

                //Check Database PHONGBAN
                case Constant.DB4OFILENAME_PHONGBAN:
                    //open data
                    db = connDB4o(database);

                    ObjectSet<Phongban> result_pb = db.query(Phongban.class);

                    while (result_pb.hasNext()) {

                        Phongban pb = (Phongban) result_pb.next();

                        if (pb.getMaPB().equals(id)) {
                            check = true;
                            break;
                        }
                    }

                    break;

                //Check Database THE
                case Constant.DB4OFILENAME_THE:
                    //open data
                    db = connDB4o(database);

                    ObjectSet<The> result_the = db.query(The.class);

                    while (result_the.hasNext()) {

                        The the = (The) result_the.next();

                        if (the.getMathe().equals(id)) {
                            check = true;
                            break;
                        }
                    }

                    break;

                //Check Database DOCGIA
                case Constant.DB4OFILENAME_DOCGIA:
                    //open data
                    db = connDB4o(database);

                    ObjectSet<Docgia> result_dg = db.query(Docgia.class);

                    while (result_dg.hasNext()) {

                        Docgia the = (Docgia) result_dg.next();

                        if (the.getMathe().equals(id)) {
                            check = true;
                            break;
                        }
                    }

                    break;

                //Default Database    
                default:
                    break;

            }


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            db.close();
        }

        return check;

    }

    /**
     * update database
     * @param database
     * @param nv
     * @param pb
     * @param the 
     */
    public static void updateData(String database, NhanVien nv, Phongban pb, The the, Docgia dg) {
        ObjectContainer db = null;

        try {

            switch (database) {

                //update NHANVIEN
                case Constant.DB4OFILENAME_NHANVIEN:
                    db = connDB4o(database);

                    ObjectSet<NhanVien> result = db.query(NhanVien.class);

                    while (result.hasNext()) {

                        NhanVien nv_ = (NhanVien) result.next();

                        if (nv_.getMaNV().equals(nv.getMaNV())) {
                            nv_.setChucvu(nv.getChucvu());
                            nv_.setDiachi(nv.getDiachi());
                            nv_.setEmail(nv.getEmail());
                            nv_.setGioitinh(nv.getGioitinh());
                            nv_.setHoten(nv.getHoten());
                            nv_.setMobile(nv.getMobile());
                            nv_.setNgaysinh(nv.getNgaysinh());
                            nv_.setPhongban(nv.getPhongban());
                            //Check PhongBan
                            Boolean check = chkData(Constant.DB4OFILENAME_PHONGBAN, nv.getPhongban());
                            if (check.booleanValue()) {
                                //update
                                db.store(nv_);

                            } else {
                                QLyTVView.showMessage("Mã phòng ban:" + nv.getPhongban() + " không tồn tại trong hệ thống");
                            }
                            break;
                        }
                    }

                    break;

                //update PHONGBAN
                case Constant.DB4OFILENAME_PHONGBAN:
                    db = connDB4o(database);

                    ObjectSet<Phongban> result_pb = db.query(Phongban.class);

                    while (result_pb.hasNext()) {

                        Phongban pb_ = (Phongban) result_pb.next();

                        if (pb_.getMaPB().equals(pb.getMaPB())) {
                            pb_.setTenPB(pb.getTenPB());
                            pb_.setGhichu(pb.getGhichu());
                            //update
                            db.store(pb_);
                            break;
                        }
                    }

                    break;

                //update THE
                case Constant.DB4OFILENAME_THE:
                    db = connDB4o(database);
                    ObjectSet<The> result_the = db.query(The.class);

                    while (result_the.hasNext()) {

                        The the_ = (The) result_the.next();

                        if (the_.getMathe().equals(the.getMathe())) {
                            the_.setLoaithe(the.getLoaithe());
                            the_.setGhichu(the.getGhichu());
                            //update
                            db.store(the_);
                            break;
                        }
                    }

                    break;

                //update DOCGIA
                case Constant.DB4OFILENAME_DOCGIA:
                    db = connDB4o(database);
                    ObjectSet<Docgia> result_dg = db.query(Docgia.class);

                    while (result_dg.hasNext()) {

                        Docgia dg_ = (Docgia) result_dg.next();

                        if (dg_.getMathe().equals(dg.getMathe())) {

                            dg_.setMathe(dg.getMathe());
                            dg_.setTrangthaithe(dg.getTrangthaithe());
                            dg_.setDiachi(dg.getDiachi());
                            dg_.setEmail(dg.getEmail());
                            dg_.setGioitinh(dg.getGioitinh());
                            dg_.setHoten(dg.getHoten());
                            dg_.setMobile(dg.getMobile());
                            dg_.setNgaysinh(dg.getNgaysinh());

                            //Check The
                            Boolean check = chkData(Constant.DB4OFILENAME_THE, dg.getMathe());
                            if (check.booleanValue()) {
                                //update
                                db.store(dg_);
                            } else {
                                QLyTVView.showMessage("Mã thẻ:" + dg.getMathe() + " không tồn tại trong hệ thống");
                            }

                            break;
                        }
                    }

                    break;

                //Default Database    
                default:
                    break;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            db.close();
        }

    }

    /**
     * Delete data
     * @param database
     * @param id 
     */
    public static void delData(String database, String id) {
        ObjectContainer db = null;

        try {

            switch (database) {
                //Delete NHANVIEN
                case Constant.DB4OFILENAME_NHANVIEN:

                    db = connDB4o(database);

                    ObjectSet<NhanVien> result = db.query(NhanVien.class);

                    while (result.hasNext()) {

                        NhanVien nv1 = (NhanVien) result.next();

                        if (nv1.getMaNV().equals(id)) {
                            //update
                            db.delete(nv1);
                            break;
                        }
                    }


                    break;

                //Delete PHONGBAN
                case Constant.DB4OFILENAME_PHONGBAN:
                    //open data
                    db = connDB4o(database);
                    //check data                                       

                    ObjectSet<Phongban> result_pb = db.query(Phongban.class);

                    while (result_pb.hasNext()) {

                        Phongban nv = (Phongban) result_pb.next();

                        if (nv.getMaPB().equals(id)) {
                            //delete
                            db.delete(nv);
                        }
                    }

                    break;

                //Delete THE
                case Constant.DB4OFILENAME_THE:
                    //open data
                    db = connDB4o(database);

                    ObjectSet<The> result_the = db.query(The.class);

                    while (result_the.hasNext()) {

                        The nv = (The) result_the.next();

                        if (nv.getMathe().equals(id)) {
                            //delete
                            db.delete(nv);
                        }
                    }

                    break;

                //Delete DOCGIA
                case Constant.DB4OFILENAME_DOCGIA:
                    //open data
                    db = connDB4o(database);

                    ObjectSet<Docgia> result_dg = db.query(Docgia.class);

                    while (result_dg.hasNext()) {

                        Docgia dg = (Docgia) result_dg.next();

                        if (dg.getMathe().equals(id)) {
                            //delete
                            db.delete(dg);
                        }
                    }

                    break;

                default:
                    break;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            db.close();
        }

    }
}
